* Load the data file -----------------------------------------------------------

	if "$individual_run" != "Yes" {
	di "Program Stopped: Make sure you load the parameters in master.do"
	e
	}

	use $interim_data, clear

* Variable list ----------------------------------------------------------------
	
	* Demographic data 
	gl demo_var age experience dmale dblack dhispanic dwhite dother
	
	* Outcome as baseline - raw and index
	gl base_var_raw ///
						pre_trr_C pre_l12_rc_trr_C ///
						pre_trr_s_i_C pre_trr_sa_injury_C pre_trr_s_h_C ///
						pre_days_iod ///
						pre_arrest_con_cop ///
						pre_total_w_exo_accusations ///
						pre_award
	
	gl base_var_index_a	pre_prs_score_w_arrs pre_ttt_score 
	gl base_var_index_e	pre_prs_score_w_arrs_e pre_ttt_score_e
	
	* Combine into a large list
	gl all_vars		$demo_var $base_var_raw $base_var_index_a
	gl all_vars_e	$demo_var $base_var_raw $base_var_index_e
	
	
* Balance table" Descriptive statistics, t-test, and F-test --------------------
// Technical note: Balance test is of the following form
//	qui: reg `v' treatment i.strata, vce(cluster employee_id)
	
	
	* All outcomes, For all officers 
	
	
	
		* Descriptive Statistics (see [functions] for the command bal_row)
		foreach outcome in $all_vars {
		bal_row `outcome' i BA_`outcome'
		}
		
			/* Note on "i": "i" is a binary variable that take on 1 for only
			   one row for a given officer. Since our data is structured 
			   to be at officer-month level, this indicator is needed 
			   to ensure calculation is at the individual level */
			
			* F-test
			qui: reg treatment $all_vars i.strata ///
			if (i == 1), vce(cluster employee_id)
			qui: test $all_vars
			gl BA_f_test_p: di %5.4f r(p)
			gl BA_f_test_N: di %9.0fc e(N)
				
	
	* All outcomes, but restrict to only officers with Endline assessment
	preserve
	keep if no_endline == 0
		
		* Descriptive statistics 
		foreach outcome in $all_vars_e{
		bal_row `outcome' i BE_`outcome'
		}
		
			* F-test
			qui: reg treatment $all_vars_e i.strata ///
			if (i == 1), vce(cluster employee_id)
			qui: test $all_vars_e
				gl BE_f_test_p: di %5.4f r(p)
				gl BE_f_test_N: di %9.0fc e(N)
		
	restore
		
	
	
	
* Balance table LATEX WRITING --------------------------------------------------

	* Appendix B1
	table_start_customized_c ///
	${report_folder}/randomization_balance.txt 	4 ///
	"Balance on Key Covariates (Full Sample)" ///
	"& \textbf{Control Mean} & \textbf{Treatment Mean} & \textbf{Difference} & \textbf{N} \\ " ///
	150
	
		midrule
		file write table "\multicolumn{5}{c}{\textbf{Panel A: Officer characteristics}} \\ "
		
		bal_row_write 	BA_age 								"Age"
		bal_row_write 	BA_experience 						"Years of experience"
		
		bal_row_write 	BA_dmale 							"Gender: Male"
		bal_row_write 	BA_dblack 							"Race and ethnicity: Black"
		bal_row_write 	BA_dhispanic 						"Race and ethnicity: Hispanic"
		bal_row_write 	BA_dwhite 							"Race and ethnicity: White"
		bal_row_write 	BA_dother 							"Race and ethnicity: Other"
		midrule
		
		file write table "\multicolumn{5}{c}{\textbf{Panel B: Officer performance prior to treatment} } \\ "
		
		bal_row_write 	BA_pre_trr_C						"Uses of force"
		bal_row_write 	BA_pre_l12_rc_trr_C					"Uses of force - All but lethal"
		bal_row_write 	BA_pre_trr_s_i_C					"Subject injuries (officer reported)"
		bal_row_write 	BA_pre_trr_sa_injury_C				"Subject allegation of injuries"
		bal_row_write	BA_pre_days_iod						"Officer injuries (days off)"
		bal_row_write	BA_pre_ttt_score	 				"Tactics used in TRRs (index)" "" index
		
		bal_row_write 	BA_pre_arrest_con_cop				"Discretionary arrests"
		bal_row_write 	BA_pre_prs_score_w_arrs				"Total officer activities (index)" "" index
		
		bal_row_write	BA_pre_total_w_exo_accusations 		"Complaints"
		bal_row_write	BA_pre_trr_s_h_C					"Hospitalizations"
		bal_row_write	BA_pre_award						"Awards and commendations"
		midrule
		
	file write table "F-test: p-value = $BA_f_test_p  & & & & $BA_f_test_N\\"
	
	table_end "$admin_balance_end" rnd_balance
	
	* Appendix B2
	table_start_customized_c ///
	${report_folder}/randomization_balance_endline.txt 	4 ///
	"Balance on Key Covariates (Endline Assessment Sample)" ///
	"& \textbf{Control Mean} & \textbf{Treatment Mean} & \textbf{Difference} & \textbf{N} \\ " ///
	150
	
		midrule
		file write table "\multicolumn{5}{c}{\textbf{Panel A: Officer characteristics}} \\ "
		
		bal_row_write 	BE_age 								"Age"
		bal_row_write 	BE_experience 						"Years of experience"
		
		bal_row_write 	BE_dmale 							"Gender: Male"
		bal_row_write 	BE_dblack 							"Race and ethnicity: Black"
		bal_row_write 	BE_dhispanic 						"Race and ethnicity: Hispanic"
		bal_row_write 	BE_dwhite 							"Race and ethnicity: White"
		bal_row_write 	BE_dother 							"Race and ethnicity: Other"
		midrule
		
		file write table "\multicolumn{5}{c}{\textbf{Panel B: Officer performance prior to treatment} } \\ "
		
		bal_row_write 	BE_pre_trr_C						"Uses of force"
		bal_row_write 	BE_pre_l12_rc_trr_C					"Uses of force - All but lethal"
		bal_row_write 	BE_pre_trr_s_i_C					"Subject injuries (officer reported)"
		bal_row_write 	BE_pre_trr_sa_injury_C				"Subject allegation of injuries"
		bal_row_write	BE_pre_days_iod						"Officer injuries (days off)"
		bal_row_write	BE_pre_ttt_score_e	 				"Tactics used in TRRs (index)" "" index
		
		bal_row_write 	BE_pre_arrest_con_cop				"Discretionary arrests"
		bal_row_write 	BE_pre_prs_score_w_arrs_e			"Total officer activities (index)" "" index
		
		bal_row_write	BE_pre_total_w_exo_accusations 		"Complaints"
		bal_row_write	BE_pre_trr_s_h_C					"Hospitalizations"
		bal_row_write	BE_pre_award						"Awards and commendations"
		midrule
		
	file write table "F-test: p-value = $BE_f_test_p  & & & & $BE_f_test_N\\"
	
	table_end "$admin_balance_end_endline" rnd_balance_endline